local linji = fk.CreateSkill{
  name = "xiaobai__linji",
  tags = {Skill.Compulsory}
}

Fk:loadTranslationTable{
  ["xiaobai__linji"] = "凛脊",
  [":xiaobai__linji"] = "你的体力值每回合首次发生变化时，当前回合角色须将两张牌置于牌堆顶或牌堆底，"..
  "然后其可以从另一端摸两张牌，防止此体力变化。",
  ["#linji_chooseCard"] = "凛脊",
  ["#linji_setCards"] = "凛脊：将你的两张牌置于牌堆顶或牌堆底",
  ["#linji_askForDraw"] = "凛脊：是否从 %arg 摸两张牌防止 %dest 本次体力变化"
}

local linji_chooseCard = fk.CreateSkill{
  name = "#linji_chooseCard",
}

linji_chooseCard:addEffect("active",{
  interaction = function (self)
    return UI.ComboBox{
      choices = {"Top","Bottom"},
      default = "Top"
    }
  end,
  prompt = "#linji_setCards",
  card_num = 2,
  card_filter = function (self, player, to_select, selected, selected_targets)
    return not selected or #selected < 2
  end,
  target_filter = Util.FalseFunc,
})

linji:addEffect(fk.BeforeHpChanged, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    if target ~= player or not player:hasSkill(self) then
      return false
    end
    local evt = player.room.logic:getEventsOfScope(GameEvent.ChangeHp, 2, function (e)
      if e.data.who == player then
        return true
      end
    end,Player.HistoryTurn)
    return evt == nil or #evt < 2
  end,
  on_cost = function (self, event, target, player, data)
    local skill_target = player.room.current
    if #skill_target:getCardIds("he") < 2 then
      return 
    end
    local _,ret = player.room:askToUseActiveSkill(skill_target, {
      skill_name = "#linji_chooseCard",
      prompt = "#linji_setCards",
      cancelable = false})
    if not _ then return false end
    local cost_data = {}
    cost_data.choice = 1
    if ret.interaction == "Bottom" then
      cost_data.choice = -1
    end
    cost_data.cards = ret.cards
    event:setCostData(self, cost_data)
    return true
  end,
  on_use = function (self, event, target, player, data)
    local skill_target = player.room.current
    local cost_data = event:getCostData(self)
    local index = cost_data.choice
    local cards = cost_data.cards
    if cards and #cards>0 then
      player.room:moveCards({
        ids = cards,
        from = skill_target.id,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonJustMove,
        skillName = "linji",
        drawPilePosition = index,
      })
    end
    local pos = "Top"
    if  index == 1 then pos = "Bottom" end
    if player.room:askToSkillInvoke(skill_target,{
        skill_name = linji.name,
        prompt = "#linji_askForDraw::"..player.id..":"..Fk:translate(pos)}) then
      if index == 1 then
        skill_target:drawCards(2, linji.name, "bottom")
      else
        skill_target:drawCards(2, linji.name, "top")
      end
      data.prevented = true
    end
  end
})

local skels = {}
table.insertTable(skels, {linji, linji_chooseCard})

return skels